package com.orphan.marketFront.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orphan.marketFront.mapper.ShippingAddressMapper;
import com.orphan.marketFront.po.ShippingAddress;
import com.orphan.marketFront.service.IShippingAddressService;
import com.orphan.util.dto.ShippingAddressDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IShippingAddressServiceImpl implements IShippingAddressService {

    @Autowired
    private ShippingAddressMapper addressMapper;

    /**
     * 根据用户ID查询用户所有收货地址
     * @param userId 用户ID
     * @return 用于显示的收货地址集合
     */
    public List<ShippingAddressDto> selectShippingAddress(Integer userId) {
        QueryWrapper<ShippingAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShippingAddress> shippingAddressList = addressMapper.selectList(wrapper);
        ArrayList<ShippingAddressDto> list = new ArrayList<>();
        for (int i = 0; i < shippingAddressList.size(); i++) {
            ShippingAddressDto dto = parseToDto(shippingAddressList.get(i));
            list.add(dto);
        }
        return list;
    }

    /**
     * 新增收货地址
     * @param shippingAddressDto 要新增的收货地址
     * @return 新增是否成功
     */
    public Boolean insertShippingAddress(ShippingAddressDto shippingAddressDto) {
        ShippingAddress address = parse(shippingAddressDto);
        int result = addressMapper.insert(address);
        if(result > 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据地址ID删除收货地址
     * @param addressId 收货地址ID
     * @return 删除是否成功
     */
    public Boolean deleteShippingAddressById(String addressId) {
        int result = addressMapper.deleteById(addressId);
        if(result > 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改收货地址
     * @param shippingAddressDto 修改后的收货地址
     * @return 修改是否成功
     */
    public Boolean updateShippingAddress(ShippingAddressDto shippingAddressDto) {
        ShippingAddress address = parse(shippingAddressDto);
        int result = addressMapper.updateById(address);
        if(result > 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     *  根据用户ID查询默认收货地址
     * @param userId 用户ID
     * @return 默认收货地址
     */
    public ShippingAddress selectDefaultShippingAddress(Integer userId) {
        QueryWrapper<ShippingAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("address_default",1);
        ShippingAddress address = addressMapper.selectOne(wrapper);
        return address;
    }

    /**
     * 修改默认收货地址
     * @param shippingAddressDto 新的默认收货地址
     * @return 修改是否成功
     */
    public Boolean updateDefaultAddress(ShippingAddressDto shippingAddressDto) {
        //查询当前默认地址
        ShippingAddress address = selectDefaultShippingAddress(shippingAddressDto.getUserId());
        Boolean flag1=false,flag2=false;
        if(address != null){
            address.setAddressDefault(0);
            flag1 = updateShippingAddress(parseToDto(address));
        }
        shippingAddressDto.setIsDefault(1);
        flag2 = updateShippingAddress(shippingAddressDto);
        if(flag2){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 传输对象转化为数据库对象
     * @param shippingAddressDto 传输对象
     * @return 数据库对象
     */
    public ShippingAddress parse(ShippingAddressDto shippingAddressDto){
        ShippingAddress shippingAddress = new ShippingAddress();
        shippingAddress.setAddressId(shippingAddressDto.getAddressId());
        shippingAddress.setConsignee(shippingAddressDto.getConsignee());
        shippingAddress.setAddressArea(shippingAddressDto.getAddressArea());
        shippingAddress.setAddressDetail(shippingAddressDto.getDetailedAddress());
        shippingAddress.setConsigneeTel(shippingAddressDto.getConsigneeTel());
        shippingAddress.setAddressDefault(shippingAddressDto.getIsDefault());
        shippingAddress.setUserId(shippingAddressDto.getUserId());
        return shippingAddress;
    }

    /**
     * 数据库对象转换为传输对象
     * @param shippingAddress 数据库对象
     * @return 传输对象
     */
    public ShippingAddressDto parseToDto(ShippingAddress shippingAddress){
        ShippingAddressDto shippingAddressDto = new ShippingAddressDto();
        shippingAddressDto.setAddressId(shippingAddress.getAddressId());
        shippingAddressDto.setConsignee(shippingAddress.getConsignee());
        shippingAddressDto.setAddressArea(shippingAddress.getAddressArea());
        shippingAddressDto.setDetailedAddress(shippingAddress.getAddressDetail());
        shippingAddressDto.setConsigneeTel(shippingAddress.getConsigneeTel());
        shippingAddressDto.setIsDefault(shippingAddress.getAddressDefault());
        shippingAddressDto.setUserId(shippingAddress.getUserId());
        return shippingAddressDto;
    }
}
